<template>
	<el-dialog :visible.sync="addPointShow" @close="$emit('update:showAddpoint',false)" width="55%" :close-on-click-modal="false" :show-close='false' :append-to-body='true'>
		<span slot="title" class="dialog-title">
		  	{{addPointTitle}}
			<el-button  size="mini" class="popupClose" @click="resetPointForm()">关闭</el-button>
			  </span>
		<!--{{sampleForm}}-->
		<el-form label-width="110px" :inline="false" class="demo-form-inline" :model="sampleForm" :rules="sampleRules" ref="sampleForm">
			<div v-if="addPointTitle!='添加现场质控'">
				<!--<el-form-item label="样品名称" prop="name">
            <el-col :span="22">
              <el-input size="mini" placeholder="项目" v-model="sampleForm.name"></el-input>
            </el-col>
          </el-form-item>-->
				<el-form-item label="检测区域" prop="monitorZone">
					<el-col :span="22">
						<el-select clearable filterable @change="monitorZoneChange" v-model="sampleForm.monitorZone" placeholder="请确认基础信息客户栏是否已选择！"
						 style="width: 100%;" size='mini'>
							<el-option v-for="item in areaoptions" :key="item.id" :label="item.name" :value="item.id">
							</el-option>
						</el-select>
					</el-col>
				</el-form-item>

				<el-form-item label="监测点类型" prop="monitorType">
					<el-col :span="22">
						<el-select clearable filterable @change="monitorTypeChange" v-model="sampleForm.monitorType" placeholder="请选择监测点类型！" style="width: 100%;"
						 size='mini'>
							<el-option v-for="item in monitorSelect.itemList" :key="item.code" :label="item.name" :value="item.code">
							</el-option>
						</el-select>
					</el-col>
				</el-form-item>

				<!--@change="monitorChange"单选时使用-->
				<el-form-item label="监测点" prop="monitor">
					<el-col :span="22">
						<el-select filterable :multiple="addPointTitle=='添加' || addPointTitle=='复制'" v-model="sampleForm.monitor" @change="monitorChange" placeholder="请选择"
						 style="width: 100%;" size='mini'>
							<el-option v-for="item in typeOptions" :key="item.id" :label="item.name" :value="item.id">
							</el-option>
						</el-select>
					</el-col>
				</el-form-item>

				<!--<el-form-item label="监测点类型" prop="monitorType">
            <el-col :span="22">
              <el-select
              	disabled
              	v-model="sampleForm.monitorType"
              	placeholder="请选择"
              	style="width: 100%;"
              	size = 'mini' >
						<el-option v-for="item in monitorSelect.itemList"
										:key="item.code"
										:label="item.name"
										:value="item.code">
						</el-option>
				</el-select>
            </el-col>
          </el-form-item>-->
				<!--   {{EmployeeByLabs}}-->
				<!--<el-form-item label="现场负责人" prop="collectionLeader">
            <el-col :span="22">
            	<el-select
              	filterable
              	@change="leaderChange"
              	v-model="sampleForm.collectionLeader"
              	placeholder="请选择"
              	style="width: 100%;"
              	size = 'mini' >
						<el-option v-for="item in EmployeeByLabs"
										:key="item.account"
										:label="item.name"
										:value="item.account">
						</el-option>
				</el-select>
            </el-col>
          </el-form-item>-->
				<!--<el-form-item label="采样人" prop="collectionMan">
            <el-col :span="22">
            	<el-select
              	filterable
              	@change="manChange"
              	v-model="sampleForm.collectionMan"
              	placeholder="请选择"
              	style="width: 100%;"
              	size = 'mini' >
						<el-option v-for="item in EmployeeByLabs"
										:key="item.account"
										:label="item.name"
										:value="item.account">
						</el-option>
				</el-select>
            </el-col>
          </el-form-item>-->
			</div>


			<el-form-item label="质控类型" v-if="addPointTitle=='添加现场质控'">
				<el-col :span="22">
					<el-radio-group v-model="sampleForm.qcType">
						<el-radio v-for="qc in QCS" :label="qc.code" :key="qc.id" :disabled="selectedArr.length==0">{{qc.name}}</el-radio>
					</el-radio-group>
					<br />
					<el-radio-group v-model="sampleForm.qcType">
						<el-radio v-for="qc in QCS1" :label="qc.code" :key="qc.id" :disabled="selectedArr.length!=0">{{qc.name}}</el-radio>
					</el-radio-group>
				</el-col>
			</el-form-item>
			<!--<el-form-item  v-if="qcValue" >
            <el-col :span="22">

            </el-col>
          </el-form-item>-->

		</el-form>
		<span slot="footer" class="dialog-footer">
			    <el-button type="primary" @click="addSubmit" size="mini">确 定</el-button>
			    <el-button  size="mini" @click="resetPointForm()">取 消</el-button>
			  </span>
	</el-dialog>
</template>

<script>
	import { mapState } from 'vuex'
	export default {
		props: ['showAddpoint', 'addPointTitle', 'sampleForm', 'addIndex', 'selectedArr', 'client', 'currentArr'],
		data() {
			return {
				checkedQC: [],
				newOrEdit: 'new',
				sampleRules: {
					monitorZone: [
						{ required: true, message: '请选择检测区域', trigger: 'change' },],
					monitor: [
						{ required: true, message: '请选择监测点', trigger: 'change' },],

				},
				typeOptions: [],
				monitorTypeArr: [],
				qcValue: false,
			}
		},
		computed: {
			...mapState(['typeQcSelect', 'areaoptions', 'monitorSelect', 'EmployeeByLabs']),
			addPointShow: {
				get: function () {

					return this.showAddpoint
				},
				set: function (newValue) {

				}
			},
			QCS() {
				//				return this.$store.state.typeQcSelect.itemList.filter(item => item.genre===2 )
				return this.$store.state.QCS
			},
			QCS1() {
				//				return this.$store.state.typeQcSelect.itemList.filter(item => item.genre===1 )
				return this.$store.state.QCS1
			}
		},
		mounted() {
			this.$store.dispatch('dogetDictionary')
		},
		destroyed() {
			// this.$store.commit('resertAreaoptions')
		},
		methods: {
			getName(value) {
				let filterArr = [];
				filterArr = this.EmployeeByLabs.filter(item => item.account === value)
				if (filterArr.length != 0) {
					return filterArr[0].name
				}
			},
			leaderChange(val) {

				this.sampleForm.collectionLeaderName = this.getName(val)
			},
			manChange(val) {

				this.sampleForm.collectionManName = this.getName(val)
			},
			getQcName(code) {
				this.typeQcSelect.itemList = [...this.QCS, ...this.QCS1]
				return this.typeQcSelect.itemList.filter(item => item.code === code)
			},
			monitorChange(id) {
				for (let i of this.typeOptions) {
					if (i.id == id) {
						return [i.monitorType, i.name]
					}
				}

			},

			monitorZoneChange(val) {

				this.$set(this.sampleForm, 'monitorZoneName', this.areaoptions.filter(item => item.id === val)[0].name)
				this.sampleForm.monitor = [];
				this.sampleForm.monitorType = "";

				this.readMonitors({ monitorZone: val })

			},
			//			monitorChange(val){
			//				this.sampleForm.monitorName = this.areaoptions.filter(item => item.id===val)[0].name
			//				this.$set(this.sampleForm,'monitorName',this.typeOptions.filter(item => item.id===val)[0].name)
			//			},

			monitorTypeChange() {
				this.sampleForm.monitor = [];
				this.readMonitors()
			},
			//			获取监测点信息
			readMonitors(row) {
				let params = { fRef: this.sampleForm.monitorZone, id: this.client, monitorType: this.sampleForm.monitorType }
				this.$http({
					method: 'post',
					apiUrl: '/client/readMonitors.htm',
					params: params,
					isJson: true,
				}).then(res => {
					if (res.code === 0) {
						this.typeOptions = res.data
						//				          	 console.log(res)
					} else {
						//				          	reject(err)
					}
				}, )


			},

			resetPointForm() {
				this.$emit('update:showAddpoint', false)
				 this.$refs.sampleForm.resetFields();
			},
			generateMixed(n) {
				var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
				var res = "";
				for (var i = 0; i < n; i++) {
					var id = Math.ceil(Math.random() * 35);
					res += chars[id];
				}
				return res;
			},
			addSubmit() {
				let addArr = [];
				let count = 1;
				this.addPointTitle = this.addPointTitle.trim()
				if (this.addPointTitle == '更改') {

					this.resetPointForm()
					this.$parent.reload()
				} else if (this.addPointTitle == "添加" || this.addPointTitle == "复制") {
					this.$refs['sampleForm'].validate((valid) => {
						if (valid) {

							let insertData;
							var pointArr = [];
							for (let i of this.sampleForm.monitor) {
								insertData = Object.assign({}, this.sampleForm)

								insertData.monitor = i;
								insertData.monitorName = this.typeOptions.filter(item => item.id === i)[0].name
								insertData.monitorType = this.monitorChange(i)[0];
								insertData.name = this.monitorChange(i)[1];
								insertData.parent = this.generateMixed(32);
								insertData.detectBeans = JSON.parse(JSON.stringify(insertData.detectBeans))
								for(var j in this.$parent.getPlanData.sampleBeans){
									if(this.$parent.getPlanData.sampleBeans[j].monitor != insertData.monitor){

									}
								}
								pointArr.push(insertData)
							}
              console.log(pointArr)
							this.$parent.getPlanData.sampleBeans = this.util.repeatData({ old: this.$parent.getPlanData.sampleBeans, new: pointArr, unique: 'monitor',sort:"order" })
							this.$parent.setSampleCurrent(this.$parent.getPlanData.sampleBeans[0])
							this.resetPointForm()
              console.log(this.$parent.getPlanData.sampleBeans)
						} else {
							console.log('error submit!!');
							return false;
						}
					});
				} else {
					// this.$parent.monitorPoints勾选的监测点数据
					console.log(this.currentArr,this.sampleForm)
					if(this.sampleForm.qcType == undefined || this.sampleForm.qcType == "" || this.sampleForm.qcType == null){
						this.$message({
							type:'info',
							message:'请选择现场质控！'
						})
						return;
					}
					if (this.$parent.monitorPoints.length == 0) {
						console.log(this.currentArr,this.sampleForm)
						// if(this.currentArr.monitorName ==undefined){
							this.sampleForm.name = this.getQcName(this.sampleForm.qcType)[0].name;

						// }else{
							// this.sampleForm.name =this.currentArr.monitorName + '-' + this.getQcName(this.sampleForm.qcType)[0].name;
						// }
						// this.sampleForm.monitor = this.currentArr.monitor
						// this.sampleForm.monitorName = this.currentArr.monitorName
						// this.sampleForm.monitorZone = this.currentArr.monitorZone
						// this.sampleForm.monitorZoneName = this.currentArr.monitorZoneName
            this.sampleForm.group = 2; //无父样
						this.sampleForm.parent = this.generateMixed(32);
						this.$parent.getPlanData.sampleBeans.unshift(this.sampleForm)
					} else {

						for (let i = 0; i < this.$parent.getPlanData.sampleBeans.length; i++) {
							for (let j = 0; j < this.$parent.monitorPoints.length; j++) {

								if (this.$parent.getPlanData.sampleBeans[i].qcType == undefined) {
									this.$parent.getPlanData.sampleBeans[i].qcType = ''
								}
								if (this.$parent.monitorPoints[j].qcType == undefined) {
									this.$parent.monitorPoints[j].qcType = ''
								}
								if (this.$parent.getPlanData.sampleBeans[i].parent == this.$parent.monitorPoints[j].parent && this.$parent.getPlanData.sampleBeans[i].qcType == '' && this.$parent.monitorPoints[j].qcType == '') {

									let item = {};
									let nextAddPoint;
									nextAddPoint = Object.assign({}, this.$parent.monitorPoints[j])
									nextAddPoint.name = this.$parent.monitorPoints[j].name + '-' + this.getQcName(this.sampleForm.qcType)[0].name;
									nextAddPoint.qcType = this.sampleForm.qcType
                  nextAddPoint.group = 1; //有父样
                  nextAddPoint.id = null;
                  console.log(nextAddPoint)
									item.index = i + count;
									item.arry = nextAddPoint;
									addArr.push(item)
									count++
								}
							}
						}

						for (let item of addArr) {
							this.$parent.getPlanData.sampleBeans.splice(item.index, 0, item.arry)
						}
					}
					this.$parent.$refs.sampleTable.clearSelection();
					this.$parent.$refs.sampleTable.setCurrentRow();
					this.$parent.currentArr = {};
					this.resetPointForm()
				}


			}
		}
	}

</script>

<style>

</style>
